#ifndef CEPOLLSERVERTHREAD_H
#define CEPOLLSERVERTHREAD_H

#include <QObject>
#include <QMap>
#include <QVector>
#include <QAtomicInt>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <stdlib.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>

#include <errno.h>
#include <fcntl.h>

#include <sys/socket.h>
#include <sys/epoll.h>
#include <netdb.h>

#include "qtcpthreadbase.h"

#define DEF_BUFFER_LENGTH   1024
#define DEF_EPOLL_SIZE      1024

class CEpollServerThread : public QTcpThreadBase
{
    Q_OBJECT

public:
    explicit CEpollServerThread();
    virtual ~CEpollServerThread();

    //启动TCP
    bool startServer(int nPort);
    //停止TCP
    void stopServer();
    //是否在运行
    bool isRunning();

private:
    //返回解析结果
    bool sendRecvStatus(bool bRtn);

signals:
    void sigNewConnection(int nClientFd, sockaddr_in* stClientAddr);
    void sigReadReady(int nFd);
    void sigConnectionClosed(int nFd);
    void sigLogMsg(const int& nLevel, const QString& sMod, const QString& sMsg);

private slots:
    //工作线程
    virtual void slotWorkProc();

private:
    int                 m_nSockFd;
    int                 m_nEpfd;
    QAtomicInt          m_bRunning;
    QVector<int>        m_vecClientFd;
    quint8              m_nReplyByte0;
    quint8              m_nReplyByte1;
};

#endif // CEPOLLSERVERTHREAD_H
